package reflectionAndAnnotation.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Properties;

/**
 * @author zxl
 * @version 1.0
 * @date 2021/10/24 15:56
 * 练习
 */
public class Exercise {
    public static void main(String[] args) throws Exception {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);

        //获得ArrayList
        Class<? extends ArrayList> aClass = list.getClass();

        Method add = aClass.getMethod("add", Object.class);

        add.invoke(list, "哈哈");
        add.invoke(list, "哈哈");
        add.invoke(list, "哈哈");

        for (Object o1 : list) {
            System.out.println(o1);
        }
        a();

    }
    public static void a() throws Exception{
        ArrayList<String> objects = new ArrayList<>();
        objects.add("a");
        Class<? extends ArrayList> aClass = objects.getClass();
        ArrayList arrayList = aClass.newInstance();
        Method method = aClass.getMethod("add",Object.class);
        method.invoke(arrayList,1);
        objects.forEach(System.out::println);
    }
}
